package com.demo.jingxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.jingxi.common.Result;
import com.demo.jingxi.pojo.User;
import com.demo.jingxi.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
//    @PostMapping
//    public Result<User> login(){
//
//    }

    @Autowired
    private UserService userService;
    //登录
    @PostMapping("/login")
    public Result<Map<String,String>> login(@RequestBody User user){
        log.info("user: {}" ,user);
        if (user.getUsername() == null || "".equals(user.getUsername().trim())){
            log.info("user为空" );
            return Result.fail("user为空");

        }
        if (user.getPassword() == null || "".equals(user.getPassword())){
            log.info("密码为空");
            return Result.fail("密码为空");
        }
        Map<String,String> map = userService.login(user);
       return map != null ? Result.success(map) : Result.fail();

    }
    @GetMapping("/info")
    public Result<?> getUserInfo(@RequestParam("token") String token){
        Map<String,Object> data = userService.getUserInfo(token);
        log.info(token);
        if(data != null){
            return Result.success(data);
        }
        return Result.fail(20003,"用户信息获取失败");
    }
    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("x-Token") String token){
        userService.logut(token);
        return Result.success("注销成功");
    }
    //分页查询
    @GetMapping("/page")
    public  Result<IPage<User>> page(@RequestParam(required = false,defaultValue = "1") Integer pageNo,
                                         @RequestParam(required = false,defaultValue = "3") Integer pageSize,
                                         @RequestParam(required = false) String username,
                                         @RequestParam(required = false) Integer email
    ) {

        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.like(username != null && !"".equals(username), User::getUsername, username);
        lqw.like(email != null && !"".equals(email), User::getEmail, email);
        Page<User> page = new Page<>(pageNo,pageSize);
        IPage<User> page1 = userService.page(page, lqw);


        return Result.success(page1);


    }
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Integer id){
        User user = userService.getUserId(id);
        if (user == null) {
            return Result.fail("用户不存在");
        }

        user.setDeleted(1);  // 设置 deleted 字段为1，表示已删除
        userService.updateById(user);

        return Result.success("删除成功");
    }
    //更新数据
    @PutMapping
    public Result<String> update( @RequestBody User user){
        log.info(user.toString());

        userService.updateUser(user);

        return Result.success("员工信息修改成功");
    }
    //保存数据
    @PostMapping
    public Result<String> save( @RequestBody User user) {


//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        //获得当前登录用户的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        userService.addUser(user);
        return Result.success("新增员工成功");

    }
    @GetMapping("/{id}")
    public  Result<User> queryById(@PathVariable("id")Integer id){
        if (id != null){
            User user = userService.getUserId(id);
            return Result.success(user);

        }
        return Result.fail("id为空");
    }
    }











